/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Diagrams;
using System.Text.RegularExpressions;
using Microsoft.VisualStudio.Modeling.DslDefinition;

namespace DslEditorPowerToy.Languages
{
    /// <summary>
    /// Helper class for manipulating DSL models
    /// </summary>
    public static class ModelHelper
    {
        /// <summary>
        /// Returns the represented Model Element for the specified Shape or visual element
        /// </summary>
        /// <param name="shapeOrElement"></param>
        /// <returns></returns>
        public static ModelElement GetRepresentedElement(object shapeOrElement)
        {
            ModelElement element = null;
            if (shapeOrElement is ElementListCompartment)
                element = ((ElementListCompartment)shapeOrElement).ParentShape.Subject;
            else if (shapeOrElement is PresentationElement)
                element = ((PresentationElement)shapeOrElement).Subject;
            else if (shapeOrElement is ModelElement)
                element = (ModelElement)shapeOrElement;

            return element;
        }
        /// <summary>
        /// Returns the represented Model Elements for the specified Shapes or visual elements
        /// </summary>
        /// <param name="singleSelection"></param>
        /// <returns></returns>
        public static IList<ModelElement> GetRepresentedElements(ICollection shapesOrElements)
        {
            List<ModelElement> representedElements = new List<ModelElement>();

            //Get represented elements
            foreach (object element in shapesOrElements)
            {
                ModelElement representedElement = ModelHelper.GetRepresentedElement(element);
                if (null != representedElement)
                    representedElements.Add(representedElement);
            }

            return representedElements;
        }
        /// <summary>
        /// Replaces tokens in specified string with values from specified Dsl.
        /// Supported tokens are: 'DslName', DslDisplayName'.
        /// </summary>
        /// <param name="stringToTranslate"></param>
        /// <returns></returns>
        public static string FormatDslInfo(string input, Dsl dsl)
        {
            string text = input;
            Regex regEx = null;

            //Replace DSL.Name
            regEx = new Regex(@"\{DslName\}", RegexOptions.IgnoreCase);
            text = regEx.Replace(text, dsl.Name);

            //Replace DSL.DisplayName
            regEx = new Regex(@"\{DslDisplayName\}", RegexOptions.IgnoreCase);
            text = regEx.Replace(text, dsl.DisplayName);

            return text;
        }

    }
}
